package com.epam.qanda.web.servlet.authentication;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.epam.qanda.db.model.User;
import com.epam.qanda.db.service.UserService;

public class CheckAvailabilityServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String inputLogin = request.getParameter("inputLogin");
		String inputEmail = request.getParameter("inputEmail");
		if (inputLogin != null) {
			User user = new UserService().getUserByLogin(inputLogin);
			if (user == null) {
				response.getWriter().println(1);
			} else {
				response.getWriter().println(0);
			}
		} else if (inputEmail != null) {
			User user = new UserService().getUserByEmail(inputEmail);
			if (user == null) {
				response.getWriter().println(1);
			} else {
				response.getWriter().println(0);
			}
		}

	}

}
